package cn.dankal.flyaudio.current.post.detail;

import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewAdapter;
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewHolder;
import cn.dankal.basiclib.util.StringUtil;
import cn.dankal.business.pojo.CommentBean;
import cn.dankal.flyaudio.R;


public class CommentReplyAdapter extends BaseRecyclerViewAdapter<CommentBean.SecondCommentsBean> {

    private OnChoiceByUserToReplyListener listener;

    private String uuid;//单条评论的id

    public CommentReplyAdapter() {

    }

    public CommentReplyAdapter(String uuid) {
        this.uuid = uuid;
    }


    public CommentReplyAdapter setListener(OnChoiceByUserToReplyListener listener) {
        this.listener = listener;
        return this;
    }

    @Override
    protected int getLayoutResId(int viewType) {
        return R.layout.post_item_rv_comment_reply;
    }

    @Override
    protected BaseRecyclerViewHolder getViewHolder(ViewGroup parent, View rootView, int viewType) {
        return new ViewHolder(rootView);
    }

    public class ViewHolder extends BaseRecyclerViewHolder<CommentBean.SecondCommentsBean> {

        @BindView(R.id.tv_content)
        TextView mTvContent;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        @Override
        public void onBindData(CommentBean.SecondCommentsBean replyCase, int position) {
            itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.innerComment(replyCase.sendName,
                            String.valueOf(uuid),
                            String.valueOf(replyCase.sendUuid), position);
                }
            });


            String sender = StringUtil.safeString(replyCase.sendName);
            String reciver = StringUtil.safeString(replyCase.receiveName);
            String content = StringUtil.safeString(replyCase.content);

            String data;

            if (sender.length() > 0 && reciver.length() > 0) {
                data = sender + "回复" + reciver + "：" + content;
            } else if (sender.length() == 0) {
                sender = "";
                data = reciver + "：" + content;
            } else if (reciver.length() == 0) {
                reciver = "";
                data = sender + "：" + content;
            } else {
                // 2017/7/21-14:41 数据有误
                return;
            }

            //创建一个 SpannableString对象
            SpannableString msp = new SpannableString(data);

            int offset = 0;
            if (sender.length() > 0 && reciver.length() > 0) {
                // data = sender + "回复" + reciver + "：" + content;

                msp.setSpan(new ForegroundColorSpan(Color.parseColor("#50a2ff")),
                        0, sender.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                offset = sender.length() + 2;

                msp.setSpan(new ForegroundColorSpan(Color.parseColor("#50a2ff")),
                        offset, offset + reciver.length() + 1,
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

            } else if (sender.length() == 0) {
                // sender = "";
                // data = reciver + "：" + content;

                msp.setSpan(new ForegroundColorSpan(Color.parseColor("#50a2ff")),
                        0, reciver.length() + 1,
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

            } else if (reciver.length() == 0) {
                // reciver = "";
                // data = sender + "：" + content;

                msp.setSpan(new ForegroundColorSpan(Color.parseColor("#50a2ff")),
                        0, sender.length() + 1,
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            } else {
                // 2017/7/21-14:41 数据有误
                return;
            }


            mTvContent.setText(msp);

        }

    }


    public interface OnChoiceByUserToReplyListener {

        void innerComment(String name, String comment_id, String userId, int position);

        void praise(String uuid, String type, int position);

        void praiseCancel(String uuid, String type, int position);
    }

}
